import { GlobalEvent } from "./GlobalEvent";
import { ConstData } from "./ConstData";
import { GlobalFunction } from "./GlobalFunction";
import panelBase from "./panelBase";

export class panelMgr {

    private static _instance:panelMgr = new panelMgr();
    static Ins()
    {
        return this._instance;
    }

    private panelPrefabList:cc.Prefab[] = [];

    async init()
    {
        GlobalEvent.on(GlobalEvent.Event.openPanel, function(event){
            this.open(event.PanelId, event.Data);
        }.bind(this), this);
    }

    private async open(panelID: number, data:any)
    {
        var _panelCfg = ConstData.panelCfg[panelID];
        var _panel = this.panelPrefabList[panelID];
        if(!_panel || !cc.isValid(_panel)){
            _panel = <cc.Prefab>await GlobalFunction.asyncLoadPrefab(_panelCfg.path);
            this.panelPrefabList[panelID] = _panel;
        }
        var _panelCom = <panelBase>cc.instantiate(_panel).getComponent(_panelCfg.component)
        _panelCom._openPanel(panelID, data);
    }
}
